﻿using MapleLeaf.Application.CookBook;
using MapleLeaf.Application.CookBook.Models;
using MapleLeaf.Core;
using MapleLeaf.Core.Dto;
using MapleLeaf.Core.Extension;
using MapleLeaf.Model.CookBook.Organization.Entity;
using MapleLeaf.WebApi.CookBook.Controllers.Dto;
using MapleLeaf.WebApi.CookBook.Controllers.Paramters;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace MapleLeaf.WebApi.CookBook.Controllers.SystemManagement {
    /// <summary>
    /// 系统管理
    /// </summary>
    public class SystemController : ApiBaseController {

        private readonly SystemClient _systemClient;
        private readonly OrganizationClient _organizationClient;
        /// <summary>
        /// 构造函数
        /// </summary>
        public SystemController(OrganizationClient organizationClient, SystemClient systemClient) {
            _organizationClient = organizationClient;
            _systemClient = systemClient;
        }
        #region 字典
        /// <summary>
        /// 获取字典管理列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PaginationModel<DictVo>>> GetDictPageList(int pageIndex, int pageSize) {
            var whereExpression = Expressionable.Create<DictEntity>();
            //if (!parameter.Keyword.IsNullOrEmpty()) {
            //    whereExpression.Or(p => p.Name.Contains(parameter.Keyword));
            //    whereExpression.Or(p => p.Code.Contains(parameter.Keyword));
            //}
            var whereExp = Expressionable.Create<DictEntity>();
            var pagemodel = new PageModel() {
                PageIndex = pageIndex,
                PageSize = pageSize,
            };
            var list = await _systemClient.GetDictPageListAsync(whereExp.ToExpression(), pagemodel);

            PaginationModel<DictVo> result = new() {
                PageIndex = pageIndex,
                PageSize = pageSize,
                Total = pagemodel.TotalCount,
                List = list.Adapt<List<DictVo>>()
            };
            return Success(result);
        }
        /// <summary>
        /// 获取字典管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<DictVo>> GetDict(long id) {
            var res = await _systemClient.GetDictInfoAsync(id);
            var data = res.Adapt<DictVo>();
            return Success(data);
        }
        /// <summary>
        /// 保存字典管理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> SaveDict(DictVo data) {
            var entity = data.Adapt<DictEntity>();
            var resId = await _systemClient.SaveDictAsync(entity);
            return resId > 0 ? Success() : Fail();
        }
        /// <summary>
        /// 删除字典管理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> DeleteDict(List<long> ids) {
            var res = await _systemClient.DeleteDictAsync(ids);
            return res ? Success() : Fail();
        }
        #endregion
        #region 字典项
        /// <summary>
        /// 获取字典项管理列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<PaginationModel<DictItemVo>>> GetDictItemPageList(DictItemPageQuery query) {
            var whereExp = Expressionable.Create<DictItemEntity>();
            if (!query.DictCode.IsNullOrEmpty()) {
                whereExp = whereExp.And(p => p.DictCode == query.DictCode);
            }
            var pagemodel = new PageModel() {
                PageIndex = query.PageIndex,
                PageSize = query.PageSize
            };
            var list = await _systemClient.GetDictItemPageListAsync(whereExp.ToExpression(), pagemodel);

            PaginationModel<DictItemVo> result = new() {
                PageIndex = query.PageIndex,
                PageSize = query.PageSize,
                Total = pagemodel.TotalCount,
                List = list.Adapt<List<DictItemVo>>()
            };
            return Success(result);
        }
        /// <summary>
        /// 获取字典项管理列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<DictItemVo>>> GetDictItemList(string dictCode) {
            var whereExp = Expressionable.Create<DictItemEntity>();
            whereExp = whereExp.And(p => p.DictCode == dictCode);
            var list = await _systemClient.GetDictItemListAsync(whereExp.ToExpression());
            var result = list.Adapt<List<DictItemVo>>();
            return Success(result);
        }
        /// <summary>
        /// 获取字典项管理列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<DictItemOption>>> GetDictItemOptions(string dictCode) {
            var whereExp = Expressionable.Create<DictItemEntity>();
            whereExp = whereExp.And(p => p.DictCode == dictCode);
            var list = await _systemClient.GetDictItemListAsync(whereExp.ToExpression());
            var result = list.ConvertAll(x => new DictItemOption(x));
            return Success(result);
        }

        /// <summary>
        /// 获取字典项管理
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<DictItemVo>> GetDictItem(long id) {
            var res = await _systemClient.GetDictItemInfoAsync(id);
            var data = res.Adapt<DictItemVo>();
            return Success(data);
        }
        /// <summary>
        /// 保存字典项管理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> SaveDictItem(DictItemVo data) {
            var entity = data.Adapt<DictItemEntity>();
            var resId = await _systemClient.SaveDictItemAsync(entity);
            return resId > 0 ? Success() : Fail();
        }
        /// <summary>
        /// 删除字典项管理
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> DeleteDictItem(List<long> ids) {
            var res = await _systemClient.DeleteDictItemAsync(ids);
            return res ? Success() : Fail();
        }
        #endregion

    }
}
